﻿using Base.Infrastructure.Dto.Response;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace Base.Api.Admin.Filters
{
    public class ResultAttrbute : ActionFilterAttribute
    {
        /// <summary>
        /// 记录日志 替换返回值
        /// </summary>
        /// <param name="context"></param>
        public override void OnResultExecuting(ResultExecutingContext context)
        {
            var result = (context.Result as ObjectResult)?.Value;
            if (result is ApiResult)
            {
                var initResult = result as ApiResult;
                if (initResult?.data == null)
                {
                    context.Result = new OkObjectResult(new
                    {
                        code = initResult?.code,
                        message = initResult?.message
                    });
                }
                else
                {
                    context.Result = new OkObjectResult(new
                    {
                        code = initResult?.code,
                        message = initResult?.message,
                        initResult?.data
                    });
                }
            }
        }
    }
}
